Coverage Report

Created: 2025-05-07 21:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\tools.proto\tools.proto\dynamic\src\buffer\unsafe_buffer.rs
Line
Count
Source
1
// Copyright (c) 2025, BlockProject 3D
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification,
6
// are permitted provided that the following conditions are met:
7
//
8
//     * Redistributions of source code must retain the above copyright notice,
9
//       this list of conditions and the following disclaimer.
10
//     * Redistributions in binary form must reproduce the above copyright notice,
11
//       this list of conditions and the following disclaimer in the documentation
12
//       and/or other materials provided with the distribution.
13
//     * Neither the name of BlockProject 3D nor the names of its contributors
14
//       may be used to endorse or promote products derived from this software
15
//       without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
use std::ops::{Range, RangeFrom, RangeTo};
30
use crate::buffer::bytes::Bytes;
31
32
pub trait Index {
33
    type Output<'a>;
34
    fn index<'a>(self, buffer: &UnsafeBuffer<'a>) -> Self::Output<'a>;
35
}
36
37
impl Index for usize {
38
    type Output<'a> = u8;
39
0
    fn index<'a>(self, buffer: &UnsafeBuffer<'a>) -> Self::Output<'a> {
40
0
        match buffer {
41
0
            UnsafeBuffer::Owned(v) => v.index(self),
42
0
            UnsafeBuffer::Borrowed(v) => v[self]
43
        }
44
0
    }
45
}
46
47
macro_rules! impl_index {
48
    ($ty: ident) => {
49
        impl Index for $ty<usize> {
50
            type Output<'a> = UnsafeBuffer<'a>;
51
52
95
            fn index<'a>(self, buffer: &UnsafeBuffer<'a>) -> Self::Output<'a> {
53
95
                match buffer {
54
86
                    UnsafeBuffer::Owned(v) => UnsafeBuffer::Owned(v.index(self)),
55
9
                    UnsafeBuffer::Borrowed(v) => UnsafeBuffer::Borrowed(&v[self])
56
                }
57
95
            }
58
        }
59
    };
60
}
61
62
impl_index!(Range);
63
impl_index!(RangeFrom);
64
impl_index!(RangeTo);
65
66
#[derive(Debug)]
67
pub enum UnsafeBuffer<'a> {
68
    Owned(Bytes),
69
    Borrowed(&'a [u8])
70
}
71
72
impl<'a> UnsafeBuffer<'a> {
73
    #[inline(always)]
74
8
    pub fn from_copy(slice: &[u8]) -> UnsafeBuffer<'a> {
75
8
        UnsafeBuffer::Owned(Bytes::from_slice(slice))
76
8
    }
77
78
    #[inline(always)]
79
4
    pub fn with_capacity(capacity: usize) -> UnsafeBuffer<'a> {
80
4
        UnsafeBuffer::Owned(Bytes::with_capacity(capacity))
81
4
    }
82
83
16
    pub unsafe fn copy(&mut self, slice: &[u8]) -> bool {
84
16
        match self {
85
11
            UnsafeBuffer::Owned(v) => v.copy(slice),
86
            UnsafeBuffer::Borrowed(_) => {
87
5
                *self = UnsafeBuffer::from_copy(slice);
88
5
                false
89
            }
90
        }
91
16
    }
92
93
    #[inline(always)]
94
227
    pub fn as_bytes(&self) -> &[u8] {
95
227
        match self {
96
201
            UnsafeBuffer::Owned(v) => v.as_bytes(),
97
26
            UnsafeBuffer::Borrowed(v) => v
98
        }
99
227
    }
100
101
22
    pub fn as_bytes_mut(&mut self) -> &mut [u8] {
102
22
        match self {
103
19
            UnsafeBuffer::Owned(v) => v.as_bytes_mut(),
104
3
            UnsafeBuffer::Borrowed(v) => {
105
3
                *self = UnsafeBuffer::from_copy(v);
106
3
                self.as_bytes_mut()
107
            }
108
        }
109
22
    }
110
111
    #[inline(always)]
112
95
    pub fn index<I: Index>(&self, index: I) -> I::Output<'a> {
113
95
        index.index(self)
114
95
    }
115
116
    #[inline(always)]
117
188
    pub fn len(&self) -> usize {
118
188
        match self {
119
144
            UnsafeBuffer::Owned(v) => v.len(),
120
44
            UnsafeBuffer::Borrowed(v) => v.len()
121
        }
122
188
    }
123
124
    #[inline(always)]
125
54
    pub unsafe fn delete(&mut self) {
126
54
        match self {
127
40
            UnsafeBuffer::Owned(v) => v.delete(),
128
14
            _ => ()
129
        }
130
54
    }
131
}